package cn.cnhis.online.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.tencent.smtt.sdk.WebView;

import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;


import com.smarx.notchlib.NotchScreenManager;

import java.lang.ref.WeakReference;

import androidx.constraintlayout.widget.ConstraintLayout;
import cn.cnhis.online.BuildConfig;
import cn.cnhis.online.activity.login.contract.ILoginContract;
import cn.cnhis.online.mymodule.Utils.StatusBarUtils;
import cn.cnhis.online.mymodule.sp.MySpUtils;


public abstract class BaseActivity<V extends IView, P extends IPresenter<ILoginContract.View>> extends AppCompatActivity {

//    private final CustomerHandle mHandler = new CustomerHandle(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式
//        StatusBarUtils.setRootViewFitsSystemWindows(this,true);
        StatusBarUtils.setStatusBar(this, true, false, "#5585F5");
//        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);//竖屏
        NotchScreenManager.getInstance().setDisplayInNotch(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
//        StatusBarUtils.setStatusBar(this, false, true);
//        if (getSupportActionBar() != null) {
//            getSupportActionBar().hide();
//        }
    }

    @Override
    public MenuInflater getMenuInflater() {
        return super.getMenuInflater();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_config, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        if (item.getItemId() == R.id.menu_log) {
//            startActivity(new Intent(this, LogActivity.class));
//            return true;
//        }
        return super.onOptionsItemSelected(item);
    }

    public abstract void inited();

    public void startOterActivity(Activity activity, Class cls) {
        Intent intent = new Intent(activity, cls);
        startActivity(intent);
    }

    /*携带数据跳转*/
    public void startOterActivity(Activity activity, Class cls, Bundle bundle, String key) {
        Intent intent = new Intent(activity, cls);
        intent.putExtra(key, bundle);
        activity.startActivity(intent);
    }

    /*携带数据跳转*/
    public void startOterActivityForesult(Activity activity, Class cls, Bundle bundle, String key, int requestCode) {
        Intent intent = new Intent(activity, cls);
        intent.putExtra(key, bundle);
        activity.startActivityForResult(intent, requestCode);
    }

    public void setWebViewMargin(WebView webView) {
        int navigationBarHeight = StatusBarUtils.getNavigationBarHeight(this);
        int statusBarHeight = StatusBarUtils.getStatusBarHeight(this);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) webView.getLayoutParams();
        layoutParams.setMargins(0, statusBarHeight, 0, 0);
        webView.setLayoutParams(layoutParams);
    }

//    public void setWebViewMargin(RelativeLayout relativeLayout) {
//        int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
//        int statusBarHeight = this.getResources().getDimensionPixelSize(resourceId);
//        int statusBarHeight = StatusBarUtils.getStatusBarHeight(this);
//        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
//        layoutParams.setMargins(0, statusBarHeight, 0, 0);
//        relativeLayout.setLayoutParams(layoutParams);
//    }

    public void setThemeolor() {
        String themeColor = MySpUtils.getThemeColor(this);
        if (BuildConfig.DEBUG) Log.d("BaseActivity", "themeColor==" + themeColor);
        if (themeColor != null && !"".equals(themeColor)) {
            StatusBarUtils.setStatusBar(this, true, false, themeColor);
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
        ActivityStack.getInstance().removeActivity(this);
    }


    /* 通过监听 触摸事件 动态 隐藏 输入法*/
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    /* 获取 点击的 当前的 view 的 范围 */
    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
//            v.setFocusable(true);
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    public static class CustomerHandle extends Handler {
        private final WeakReference<BaseActivity<IView, IPresenter<ILoginContract.View>>> mActivity;

        public CustomerHandle(BaseActivity<IView, IPresenter<ILoginContract.View>> activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mActivity != null) {

                if (msg.what == 1) {
                    mActivity.get().finish();
                } else if (msg.what == 2) {
                    Toast.makeText(mActivity.get(), "msg.obj:" + msg.obj, Toast.LENGTH_SHORT).show();
                }

            }
        }

    }


}
